package com.commerceE.managedBean;


import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.submenu.Submenu;
import org.primefaces.model.DefaultMenuModel;
import org.primefaces.model.MenuModel;

import com.commerceE.util.FacesUtil;


@ManagedBean(name="adminHomeBean")
@SessionScoped
public class AdminHomeBean {
	
	private String leftPage = "";
	
	private String centerPage = "";
	
	private int activeIndex = 0;

	private MenuModel topMenuModel = new DefaultMenuModel();
	
	private MenuModel leftMenuModel = new DefaultMenuModel();
	
	static String item1_Name = "Content Management";
	
	static String item2_Name = "Customer Experence Mamagement";
	
	static String item3_Name = "Order Management";
	
	public AdminHomeBean(){
		MenuItem item1 = new MenuItem();
		
		item1.setValue(item1_Name);
		item1.setUpdate(":centerContentPanel,:leftContentPanel,:menuForm:mainTabMenu");
		item1.addActionListener(FacesUtil.createMethodActionListener("#{adminHomeBean.navigateLeftPage('"+item1_Name+"')}", Void.class,
                new Class[] {}));
		
		MenuItem item2 = new MenuItem();
		item2.setValue(item2_Name);
		item2.setUpdate(":centerContentPanel,:leftContentPanel,:menuForm:mainTabMenu");
		item2.addActionListener(FacesUtil.createMethodActionListener("#{adminHomeBean.navigateLeftPage('"+item2_Name+"')}", Void.class,
                new Class[] {}));
		
		MenuItem item3 = new MenuItem();
		item3.setValue(item3_Name);
		item3.setUpdate(":centerContentPanel,:leftContentPanel,:menuForm:mainTabMenu");
		item3.addActionListener(FacesUtil.createMethodActionListener("#{adminHomeBean.navigateLeftPage('"+item3_Name+"')}", Void.class,
                new Class[] {}));
		
		topMenuModel.addMenuItem(item1);
		topMenuModel.addMenuItem(item2);
		topMenuModel.addMenuItem(item3);
	}
 
	private DefaultMenuModel content_management_MenuModel = null;
	private DefaultMenuModel customer_management_MenuModel = null;
	
	public String navigateLeftPage(String pageName){
		if(item1_Name.equals(pageName)){
			if(content_management_MenuModel==null){
				Submenu submenu1 = new Submenu();
				submenu1.setLabel("Product Management");
				MenuItem item1 = new MenuItem();
				item1.setValue(leftMenu_ProductList_Name);
				item1.setUpdate(":centerContentPanel");
				item1.addActionListener(FacesUtil.createMethodActionListener("#{adminHomeBean.navigateCenterPage('"+leftMenu_ProductList_Name+"')}", Void.class,
		                new Class[] {}));
				
				MenuItem item3 = new MenuItem();
				item3.setValue(leftMenu_ProductTagList_Name);
				item3.setUpdate(":centerContentPanel");
				item3.addActionListener(FacesUtil.createMethodActionListener("#{adminHomeBean.navigateCenterPage('"+leftMenu_ProductTagList_Name+"')}", Void.class,
		                new Class[] {}));
				
				submenu1.getChildren().add(item1);
				submenu1.getChildren().add(item3);
				
				Submenu submenu2 = new Submenu();
				submenu2.setLabel("Category Management");
				MenuItem item2 = new MenuItem();
				item2.setValue(leftMenu_CategoyList_Name);
				item2.setUpdate(":centerContentPanel");
				item2.addActionListener(FacesUtil.createMethodActionListener("#{adminHomeBean.navigateCenterPage('"+leftMenu_CategoyList_Name+"')}", Void.class,
		                new Class[] {}));
				submenu2.getChildren().add(item2);
				content_management_MenuModel = new DefaultMenuModel();
				content_management_MenuModel.addSubmenu(submenu1);
				content_management_MenuModel.addSubmenu(submenu2);
			}
			leftMenuModel = content_management_MenuModel;
			setCenterPage("");
			activeIndex = 0;
		}else if(item2_Name.equals(pageName)){
			if(customer_management_MenuModel==null){
				Submenu submenu1 = new Submenu();
				submenu1.setLabel("User Management");
				MenuItem item1 = new MenuItem();
				item1.setValue(leftMenu_ProductList_Name);
				item1.setUpdate(":centerContentPanel");
				item1.addActionListener(FacesUtil.createMethodActionListener("#{adminHomeBean.navigateCenterPage('"+leftMenu_ProductList_Name+"')}", Void.class,
		                new Class[] {}));
				submenu1.getChildren().add(item1);
				
				Submenu submenu2 = new Submenu();
				submenu2.setLabel("History Management");
				MenuItem item2 = new MenuItem();
				item2.setValue(leftMenu_CategoyList_Name);
				item2.setUpdate(":centerContentPanel");
				item2.addActionListener(FacesUtil.createMethodActionListener("#{adminHomeBean.navigateCenterPage('"+leftMenu_CategoyList_Name+"')}", Void.class,
		                new Class[] {}));
				submenu2.getChildren().add(item2);
				customer_management_MenuModel = new DefaultMenuModel();
				customer_management_MenuModel.addSubmenu(submenu1);
				customer_management_MenuModel.addSubmenu(submenu2);
			}
			leftMenuModel = customer_management_MenuModel;
			setCenterPage("");
			activeIndex = 1;
		}else if(item3_Name.equals(pageName)){
			leftMenuModel = new DefaultMenuModel();
			setCenterPage("");
			activeIndex = 2;
		}
		return null;
	}
	

    static String leftMenu_ProductList_Name = "Product List";
    
    static String leftMenu_ProductTagList_Name = "Product Tag";
	
	static String leftMenu_CategoyList_Name = "Category List";
	
	public String navigateCenterPage(String pageName){
		System.out.println("Set the center page to: "+pageName);
		if(leftMenu_ProductList_Name.equals(pageName)){
			setCenterPage("productList.xhtml");
			activeIndex = 0;
		}else if(leftMenu_ProductTagList_Name.equals(pageName)){
			setCenterPage("productTagList.xhtml");
			activeIndex = 1;
		}else if(leftMenu_CategoyList_Name.equals(pageName)){
			setCenterPage("categoryList.xhtml");
			activeIndex = 1;
		} 
		return null;
		
	}
	
	 

	public String getLeftPage() {
		return leftPage;
	}


	public void setLeftPage(String leftPage) {
		this.leftPage = leftPage;
	}


	public String getCenterPage() {
		return centerPage;
	}


	public void setCenterPage(String centerPage) {
		this.centerPage = centerPage;
	}


	public MenuModel getTopMenuModel() {
		return topMenuModel;
	}


	public void setTopMenuModel(MenuModel topMenuModel) {
		this.topMenuModel = topMenuModel;
	}


	public MenuModel getLeftMenuModel() {
		return leftMenuModel;
	}


	public void setLeftMenuModel(MenuModel leftMenuModel) {
		this.leftMenuModel = leftMenuModel;
	}


	public int getActiveIndex() {
		return activeIndex;
	}

	public void setActiveIndex(int activeIndex) {
		this.activeIndex = activeIndex;
	}

}
